//var express = require('express');
//var router = express.Router();
//
///* GET home page. */
//router.get('/', function(req, res) {
//  res.render('index', { title: 'Express' });
//});
//
//module.exports =
var crypto = require('crypto'),
    User = require('../models/user.js'),
    Project = require('../models/project.js'),
    Act = require('../models/active.js');

module.exports = function(app) {
    app.get('/', function (req, res) {
        Project.get(null, function(err, projects){
            if(err){
                projects = [];
            }
            res.render('index', {
                title: '首页',
                user: req.session.user,
                success: req.flash('success').toString(),
                error: req.flash('error').toString(),
                pjts: projects
            });
        });

    });
    app.post('/',  function(req, res){});

    app.get ('/reg', checkLogin);
    app.get('/reg', function(req, res){
        res.render('reg', {
            title: '注册',
            user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        });
    });
    app.post('/reg', checkLogin);
    app.post('/reg', function (req, res) {
        var name = req.body.username,
            password = req.body.password,
            password_re = req.body.repeatpassword,
            word = req.body.word;

        if(word != 'tqbb'){
            req.flash('error', "邀请码错误");
            return res.redirect('/reg?1');
        }
        //检验用户两次输入的密码是否一致
        if (password_re != password) {
            req.flash('error', '两次输入的密码不一致!');
            return res.redirect('/reg');//返回注册页
        }
        //生成密码的 md5 值
        var md5 = crypto.createHash('md5'),
            password = md5.update(req.body.password).digest('hex');
        var newUser = new User({
            name: name,
            password: password
        });
        //检查用户名是否已经存在
        User.get(newUser.name, function (err, user) {
            if (user) {
                req.flash('error', '用户已存在!');
                return res.redirect('/reg');//返回注册页
            }
            //如果不存在则新增用户
            newUser.save(function (err, user) {
                if (err) {
                    req.flash('error', err);
                    return res.redirect('/reg');//注册失败返回主册页
                }
                req.session.user = user;//用户信息存入 session
                console.log(user);
                console.log(req.session.user);
                req.flash('success', '注册成功!');
                res.redirect('/');//注册成功后返回主页
            });
        });
    });
//    app.post('/reg', function(req, res){
//        var name = req.body.username,
//            password = req.body.password,
//            repassword = req.body.repeatpassword,
//            word = req.body.word;
//
//
////        if(word != 'tqbb'){
////            req.flash('error', "邀请码错误");
////            return res.redirect('/reg?1');
////        }
//
//        if(password != repassword){
//            req.flash('error', "密码错误");
//            return res.redirect('/reg?2');
//        }
//
//        var md5 =  crypto.createHash('md5'),
//            password = md5.update(req.body.password).digest('hex');
//
//        var newUser = new User({
//            name: name,
//            password: password
//        });
//
//        //checkout user
//        User.get(function(err, user){
//            if(err){
//                req.flash('error', err);
//                return res.redirect('/reg');
//            }
//            req.session.user = user;
//            req.flash('success', '注册成功');
//            res.redirect('/')
//        });
//    });

    app.get('/login', checkLogin);
    app.get('/login', function(req, res){
        res.render('login', {
            title: '登入',
            user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        });
    });
    app.post('login', checkLogin);
    app.post('/login', function(req, res){
        var md5 = crypto.createHash('md5'),
            password = md5.update(req.body.password).digest('hex');
        User.get(req.body.username,function(err, user){
            if(!user){
                req.flash('error', "用户不存在");
                return res.redirect('/login');
            }
            if(user.password != password){
                req.flash('error', "密码错误");
                return res.redirect('/login');
            }

            req.flash('success', "登入成功!");
            req.session.user = user;
            res.redirect('/');
        })
    });

    app.get('/logout', checkNotLogin);
    app.get('/logout', function(req, res){
        req.session.user = null;
        req.flash('success', "登出成功！");
        res.redirect('/');
    });
    app.post('/logout', checkNotLogin);
    app.post('/logout',function(req, res){});

    app.get('/addtable',function(req, res){
        res.render('addtable', {
            title: "add",
            user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        });
    });
    app.post('/addtable', function(req, res){});

    app.get('/check/:id', checkNotLogin);
    app.get('/check/:id',function(req, res){
        var ulrparam =req.param('id');
        ulrparam = ulrparam.slice(3)
        console.log(ulrparam);
        Act.get(ulrparam, function(err, act){
            if(err){
                return req.redirect('back');
            }
            res.render('check', {
                title: "活动详情",
                user: req.session.user,
                success: req.flash('success').toString(),
                error: req.flash('error').toString(),
                acts: act
            });
        });


    });
    app.post('/check', function(req, res){});

    app.get('/add', checkNotLogin);
    app.get('/add', function(req, res){
        res.render('add', {
            title: "添加活动",
            user: req.session.user,
            success: req.flash('success').toString(),
            error: req.flash('error').toString()
        })
    })
    app.post('/add', checkNotLogin);
    app.post('/add', function(req, res){
        if(!req.body.name){
            req.flash('error', '请输入活动名称!');
            return res.redirect('/add');
        }

        if(!req.body.other){
            req.flash('error', '请输入活动详情!');
            res.redirect('/add');
        }

        var projectData = {
            name: req.body.name,
            describe: req.body.other
        }
        var project = new Project(projectData);
        project.save(function(err, project){
            //console.log(project);
            if(err){
                req.flash('error', err);
                return res.redirect('/add');
            }
            req.flash('success', '活动添加成功!');
            res.redirect('/');
        });

    })


    app.get('/bg/:id', function(req, res){
        var ulrparam = req.param('id');
        ulrparam = ulrparam.slice(3);
        console.log(ulrparam);
        res.render('bg.ejs', {title:"test"});
    });
    app.post('/bg/:id',function(req, res){
        var data = {
            name: req.body.name,
            tel: req.body.tel,
            wxh: req.body.wxh,
            message: req.body.messages,
            hdid: req.body.hdid
        }
        if(!data.name || !data.tel ){
            return res.redirect('back');
            //return res.redirect('/bg/:id');
        }


        var active = new Act(data);
        active.save(function(err, active){
            if(err){
                return req.redirect('/login');
            }
        });

        res.redirect('/');
    });





    function checkLogin(req, res, next){
        if(req.session.user){
            req.flash('error', "已经登入！");
            res.redirect('back');
        }
        next();
    }

    function checkNotLogin(req, res, next){
        if(!req.session.user){
            req.flash('error', "未登入");
            res.redirect('/login');
        }
        next();
    }
};